﻿using System;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;

namespace _43Things
{
    public partial class Signin
    {
        private readonly IsolatedStorageSettings _userSetting = IsolatedStorageSettings.ApplicationSettings;
        public string Username { get; set; }

        public Signin()
        {
            InitializeComponent();
        }

        private void BtnLogInClick(object sender, RoutedEventArgs e)
        {
            SaveUsername();
            SavePassword();
            var credentials = new NetworkCredential(txtUSN.Text, txtPwd.Password);
            string url = string.Format("{0}{1}&password={2}",
                                       "http://www.43things.com/service/authentication_test?api_key=545008@43rtF0IcpguS2&username=",
                                       Username, txtPwd.Password);
            var createWebRequestReturnSoap = new CreateWebRequestReturnSoap(url, credentials);

            if (createWebRequestReturnSoap.IsSuccess)
            {
                Deployment.Current.Dispatcher.BeginInvoke(() => Notification.Visibility = Visibility.Visible);
            }
            else
            {
                //SaveUsername();
                Deployment.Current.Dispatcher.BeginInvoke(
                    () => NavigationService.Navigate(new Uri("/Personal.xaml", UriKind.Relative)));
            }
        }

        private void SaveUsername()
        {
            try
            {
                _userSetting["username"] = txtUSN.Text;
            }
            catch (Exception ex)
            {
                _userSetting["username"] = string.Empty;

            }
        }
        private void SavePassword()
        {
            try
            {
                _userSetting["password"] = txtPwd.Password;
            }
            catch (Exception ex)
            {
                _userSetting["password"] = string.Empty;

            }
        }
    }
}